//
//  SHBankCardListView.m
//  JiFengDaRen
//
//  Created by SH_iOS on 2017/10/23.
//  Copyright © 2017年 SH_iOS. All rights reserved.
//
#define  KCELLHEIGHT __I6Height(50.f)
#define  KMYBGVIEWHEIGHT __I6Height(300.f)

#import "BankCardModel.h"
#import "PayBankCell.h"
#import "SHBankCardListView.h"

@interface SHBankCardListView()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UIView *myBgView;
@property(nonatomic,strong)UIView *firstView;
@property(nonatomic,strong)UITableView *customTableView;//列表
@property(nonatomic,strong)UIButton *dissmissButton;//收起按钮
@property(nonatomic,strong)NSArray *dataSourceArray;//银行卡数据源
@property(nonatomic ,strong)UILabel *payBankCardLabel;//头部标题


@end
@implementation SHBankCardListView



// 初始化
- (instancetype)initWithFrame:(CGRect)frame bankCardListArray:(NSArray *)bankCardListArray{
    if (self = [super initWithFrame:frame]) {
        self.backgroundColor = [UIColor colorWithWhite:0.5 alpha:0.5];
        self.dataSourceArray = bankCardListArray;
    }
    return  self;
}



- (UIView *)myBgView{
    
    if (!_myBgView) {
        _myBgView = [[UIView alloc]initWithFrame:CGRectMake(0,KScreenHeight, KScreenWidth, KScreenHeight)];
        _myBgView.backgroundColor = KBASEBGCOLOR;
        [self addSubview:_myBgView];
        
    }
    
    return _myBgView;
}

- (UIView *)firstView{
    if (!_firstView) {
        _firstView = [[UIView alloc]initWithFrame:CGRectMake(0,0, KScreenWidth,__I6Height(60))];
        _firstView.backgroundColor = [UIColor whiteColor];
        [self.myBgView addSubview:_firstView];
        
    }
    return _firstView;
}
//懒加载 支付类型列表
- (UITableView *)customTableView{
    
    if (!_customTableView) {
        _customTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _customTableView.showsVerticalScrollIndicator = NO;
        _customTableView.delegate = self;
        _customTableView.dataSource = self;
        _customTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        [self.myBgView addSubview:_customTableView];
        [_customTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.bottom.mas_equalTo(self.myBgView.mas_bottom).with.offset(0);
            make.width.mas_equalTo(KScreenWidth);
            make.centerX.mas_equalTo(self.mas_centerX);
            make.top.mas_equalTo(self.firstView.mas_bottom).with.offset(0);
        }];
        
        [self.customTableView registerClass:[PayBankCell class] forCellReuseIdentifier:@"bankcard"];
        
    }
    return  _customTableView;
}

//懒加载 取消按钮
- (UIButton *)dissmissButton{
    
    if (!_dissmissButton) {
        _dissmissButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_dissmissButton setImage:[UIImage imageNamed:@"关闭"] forState:UIControlStateNormal];
        [_dissmissButton setTitleColor:KTEXTCOLOR  forState:UIControlStateNormal];
        _dissmissButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        [_dissmissButton addTarget:self action:@selector(dissMissCustomView) forControlEvents:UIControlEventTouchUpInside];
        [self.firstView addSubview:_dissmissButton];
        
        [_dissmissButton mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.mas_equalTo(self.firstView.mas_right).with.offset(0);
            make.centerY.mas_equalTo(self.firstView.mas_centerY);
            make.width.mas_equalTo(__I6Width(50));
            make.height.mas_equalTo(__I6Width(50));
            
        }];
    }
    return  _dissmissButton;
}
- (UILabel *)payBankCardLabel{
    
    if (!_payBankCardLabel) {
        _payBankCardLabel = [[UILabel alloc]init];
        _payBankCardLabel.text = @"请选择收款银行卡";
        _payBankCardLabel.textColor = KTEXTCOLOR;
        _payBankCardLabel.font = KSystemFont(__I6Width(16));
        _payBankCardLabel.textAlignment = NSTextAlignmentCenter;
        [self.firstView addSubview:_payBankCardLabel];
        [_payBankCardLabel mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_equalTo(self.firstView.mas_left).with.offset(__I6Width(10));
            make.right.mas_equalTo(self.mas_right).with.offset(__I6Width(0));
            make.centerY.mas_equalTo(self.firstView.mas_centerY);
            make.height.mas_equalTo(__I6Width(20));
            
        }];
        
    }
    return _payBankCardLabel;
}
// 添加视图
- (void)addSubViews{
    
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [self myBgView];
    [self firstView];
    [self customTableView];
    [self dissmissButton];
    [self payBankCardLabel];
    
}

//显示视图
- (void)showBankCardListView{
    [self addSubViews];
    CGRect frame = self.myBgView.frame;
    frame =  CGRectMake(0,KScreenHeight - KMYBGVIEWHEIGHT, KScreenWidth, KMYBGVIEWHEIGHT);
    [UIView animateWithDuration:0.2 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        self.myBgView.frame = frame;
    } completion:^(BOOL finished) {
        
        
    }];
    
}

//隐藏视图
- (void)dissMissCustomView{
    
    [self.customTableView removeFromSuperview];
    [self.dissmissButton removeFromSuperview];
    [self.myBgView removeFromSuperview];
    [self removeFromSuperview];
    
}



#pragma mark ---- UITableView Delegate & Datasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    return  self.dataSourceArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    PayBankCell *payBankCell;
    BankCardModel *model = self.dataSourceArray[indexPath.row];
    payBankCell = [tableView dequeueReusableCellWithIdentifier:@"bankcard"];
    payBankCell.model = model;
    return payBankCell;
    
    
    
}


- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return KCELLHEIGHT;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self dissMissCustomView];
    BankCardModel *model = self.dataSourceArray[indexPath.row];
    if ([_delegate respondsToSelector:@selector(bankCardListView:bankCardModel:)]) {
        [_delegate  bankCardListView:self bankCardModel:model];
    }
    
    
    
    
}
/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
